﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Collections;

namespace CalemShop
{
    public partial class FrmNewItem : Form
    {
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");
        DataTable table = new DataTable();
        private ArrayList genderData;
        private DataTable itemType;
        public Boolean isChangedData = false;
        DataRow newRow = null;

        public FrmNewItem()
        {
            InitializeComponent();
            initDatatable();
            this.BackColor = Color.White;
        }

        private void initDatatable()
        {
            // init Item type
            itemType = Database.getItemType();
            listType.DataSource = itemType;
            listType.DisplayMember = "typeName";
            listType.ValueMember = "typeId";

            // init Gender list
            genderData = new ArrayList();
            genderData.Add(new Gender("Nam", "T"));
            genderData.Add(new Gender("Nữ", "G"));
            listGender.DataSource = genderData;
            listGender.DisplayMember = "GenderName";
            listGender.ValueMember = "GenderValue";


            table.Columns.Add("itemId");
            table.Columns.Add("itemName");
            table.Columns.Add("type");
            table.Columns.Add("gender");
            table.Columns.Add("size");
            table.Columns.Add("color");
            table.Columns.Add("price");
            table.Columns.Add("cost");
            table.Columns.Add("giasi");
            table.Columns.Add("desc");
            table.Columns.Add("amount");
            bindingSource.DataSource = table;

        }

        private void btSave_Click(object sender, EventArgs e)
        {
            newRow = table.NewRow();

            newRow["itemId"] = txtMaSo.Text;

            newRow["itemName"] = txtName.Text;

            if (!string.IsNullOrEmpty(txtPrice.Text))
            {
                newRow["price"] = txtPrice.Text.Replace(".", "");
            }


            if (!string.IsNullOrEmpty(txtCost.Text))
            {
                newRow["cost"] = txtCost.Text.Replace(".", "");
            }

            if(!string.IsNullOrEmpty(txtGiaSi.Text)) {
                newRow["giasi"] = txtGiaSi.Text.Replace(".", "");
            }
            

            if (listGender.SelectedValue != null)
            {
                newRow["gender"] = listGender.SelectedValue.ToString();
            }

            if (listColor.SelectedItem != null)
            {
                newRow["color"] = listColor.SelectedItem.ToString();
            }

            newRow["amount"] = txtAmount.Text;

            newRow["size"] = txtSize.Text;

            if (listType.SelectedValue != null)
            {
                newRow["type"] = listType.SelectedValue.ToString();
            }

            newRow["desc"] = txtDesc.Text;
            table.Rows.Add(newRow);
            Database.addStockRows(newRow);
            isChangedData = true;
            
        }

        private void FrmNewItem_Load(object sender, EventArgs e)
        {
            addAutoNumFormat();
            AutoCompleteStringCollection list = new AutoCompleteStringCollection();
            list.AddRange(new String[] {"AT0001", "AT0002","AG0003", "AG0008"});
            txtMaSo.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtMaSo.AutoCompleteCustomSource = list;

            
        }


        private void txtPrice_KeyUp(object sender, KeyEventArgs e)
        {
           
        }

        private void addAutoNumFormat()
        {
            foreach (Control c in groupItems.Controls)
            {
                if (c.Tag != null && string.Compare(c.Tag.ToString(), "number", true) == 0)
                {
                    c.KeyUp += new KeyEventHandler(c_KeyUp);
                    c.KeyPress += new KeyPressEventHandler(c_KeyPress);
                }
            }
        }

        void c_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        

        void c_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
               
                if (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                {
                    TextBox control = (TextBox)sender;
                    string value = control.Text.Replace(@".", "");
                    int num;
                    Int32.TryParse(value, out num);
                    string s = num.ToString("#,0", cul.NumberFormat);
                    control.Text = s;
                    control.SelectionStart = s.Length + 1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DataRowView rowView = bindingSource.Current as DataRowView;
            Database.deleteStockRowByItemId(rowView.Row);
            rowView.Delete();
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
